﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GMap.NET;
using GMap.NET.WindowsPresentation;

namespace MapTrackerApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            mapProviders.ItemsSource = Enum.GetValues(typeof(MapType));
            mapProviders.SelectedItem = gMap.MapType;
            Singleton<GMaps>.Instance.Mode = AccessMode.ServerOnly;
            gMap.MaxZoom = 0x11;
            gMap.MinZoom = 5;
            gMap.Zoom = 12;
            zoom.Value = (int)gMap.Zoom;
            zoom.Maximum = 0x11;
            zoom.Minimum = 5;
            GeoCoderStatusCode status = GeoCoderStatusCode.Unknow;

            if (App.GetQuery != String.Empty)
            {
                var home = GMaps.Instance.GetLatLngFromGeocoder("Bielsko-Biała, Warszawska 153", out status);
                startAddress.Text = "Bielsko-Biała, Warszawska 153";
                endAddress.Text = App.GetQuery;
                if (status == GeoCoderStatusCode.G_GEO_SUCCESS)
                {
                    status = GeoCoderStatusCode.Unknow;
                    var location = GMaps.Instance.GetLatLngFromGeocoder(App.GetQuery, out status);
                    if (status == GeoCoderStatusCode.G_GEO_SUCCESS)
                    {
                        gMap.CurrentPosition = (PointLatLng)location;

                    }

                    MapRoute route = Singleton<GMaps>.Instance.GetRouteBetweenPoints((PointLatLng)home, (PointLatLng)location, false, (int)gMap.Zoom);
                    distance.Text = route.Distance.ToString("0.00 km");
                    GMapMarker item = new GMapMarker((PointLatLng)home);
                    item.Route.AddRange(route.Points);
                    item.RegenerateRouteShape(gMap);
                    item.ZIndex = -1;
                    gMap.Markers.Add(item);
                }
            }
            else
            {
                var home = GMaps.Instance.GetLatLngFromGeocoder("Bielsko-Biała", out status);
                if (status == GeoCoderStatusCode.G_GEO_SUCCESS)
                {
                    gMap.CurrentPosition = (PointLatLng)home;
                }
            }

            
        }

        private void zoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            gMap.Zoom = this.zoom.Value;
        }

        private void mapProviders_DropDownClosed(object sender, EventArgs e)
        {
            gMap.MapType = (MapType)mapProviders.SelectedItem;
        }

        private void gMap_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0 && zoom.Value < zoom.Maximum){
                zoom.Value += step;
            }
            else if (e.Delta < 0 && zoom.Value > zoom.Minimum)
            {
                zoom.Value -= step;
            }

        }

        private void gMap_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (zoom.Value < zoom.Maximum)
            {
                
                zoom.Value++;
            }
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2 && zoom.Value < zoom.Maximum)
            {
                zoom.Value++;
            }
        }

        private void gMap_OnTileLoadStart()
        {
            
            Action method = delegate
            {
                this.progress.Visibility = Visibility.Visible;
            };
            try
            {
                base.Dispatcher.BeginInvoke(method);
            }
            catch
            {
            }


        }

        private void gMap_OnTileLoadComplete()
        {
            Action method = delegate
            {
                this.progress.Visibility = Visibility.Hidden;
            };
            try
            {
                base.Dispatcher.BeginInvoke(method);
            }
            catch
            {
            }

        }

        private double step = 0.1;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
